#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<vector>

const int N = 1e5 + 5;
const int M = (1 << (int)std::ceil(std::log2(N))) + 5;

int n, m, ap[N], a[N], c[N], now[N][2];
std::vector<bool> d[M];

// Wants: 100 + 20 + 20 + 0 = 140.
// That's just a sweet dream, but of course my code is so bad that
// even I may only get:
// 100 + 10 + 20 + 0 = 130.
// Finally, my dream gone.

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(0), std::cout.tie(0);

    std::freopen("arena.in", "r", stdin);
    std::freopen("arena.out", "w", stdout);

    std::cin >> n >> m;
    for(int i = 1; i <= n; ++i) {
        std::cin >> ap[i];
    }
    for(int i = 1; i <= m; ++i) {
        std::cin >> c[i];
    }

    int k = std::ceil(std::log2(n));
    for(int i = 1; i <= k; ++i) {
        for(int j = 1; j <= (1 << (k - i)); ++j) {
            char c; std::cin >> c;
            d[i].push_back(c - '0');
        }
    }

    int t; std::cin >> t;
    for(; t; --t) {
        int ans = 0;
        int x[4] = {0};
        for(int i = 0; i < 4; ++i) {
            std::cin >> x[i];
        }

        for(int i = 1; i <= n; ++i) {
            a[i] = ap[i] ^ x[i % 4];
        }

        // Wo de ma ya, wo zhe xie de shi shen me shi shan dai ma?
        // Bu xiang tiao.

        for(int i = 1; i <= m; ++i) {
            int level = k - std::log2(c[i]);
            for(int j = 1; j <= c[i]; ++j) now[j][0] = a[j];
            for(int j = 1; j <= std::log2(c[i]) + 1; ++j) {
                for(int k = 1; k <= c[i] / (1 << j) / 2; ++k) {
                    if(d[level + j - 1][k] == 0) now[k][j & 1] = (now[2 * k - 1][!(j & 1)] > j ? now[2 * k - 1][!(j & 1)] : now[2 * k][!(j & 1)]);
                    else now[k][j & 1] = (now[2 * k][!(j & 1)] > j ? now[2 * k][!(j & 1)] : now[2 * k - 1][!(j & 1)]);
                }
            }
            ans ^= (i * now[1][(int)(std::log2(c[i]) + 1) & 1]);
        }
        std::cout << ans << "\n";
    }

    return 0;
}
